//
//  BBSViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/12.
//  Copyright © 2016年 KX. All rights reserved.
#import "FreeTravelMacro.h"
#import "ModelOfForum.h"
#import "BBSViewController.h"
#import "FreeTravelNetwork.h"
#import "ModelOfForum_thread_list.h"
#import "BBSCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "CellOfHotCommunity.h"
#import "ModelOfForum_thread_listEntry.h"
#import "ModelOfForumTypes.h"
#import "WebViewOfCommendViewController.h"
@interface BBSViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) ModelOfForum *forumModel;
@property (nonatomic, strong) ModelOfForum_thread_list *thread_listModel;
@property (nonatomic, assign) NSInteger currentInteger;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *desLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *total_threads;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@end

@implementation BBSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.oldIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    self.currentInteger = 0;
    [self handleData];
    [self createTableView];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-108) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfHotCommunity class] forCellReuseIdentifier:@"BBSTableViewCell"];
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    self.tableView.tableHeaderView = self.headView;
    self.headImageView = [[UIImageView alloc]init];
    [self.headView addSubview:_headImageView];
    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left).mas_equalTo(10);
        make.top.equalTo(self.headView.mas_top).mas_equalTo(10);
        make.width.height.mas_equalTo(80);
    }];
    self.desLabel = [[UILabel alloc]init];
    [self.headView addSubview:_desLabel];
    [_desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headImageView.mas_right).mas_offset(10);
        make.bottom.equalTo(_headImageView.mas_bottom);
       
    }];
    self.nameLabel = [[UILabel alloc]init];
    [self.headView addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headImageView.mas_right).mas_offset(10);
        make.top.equalTo(self.headView.mas_top).mas_offset(10);
    }];
    self.total_threads = [[UILabel alloc]init];
    [self.headView addSubview:_total_threads];
    [_total_threads mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_headImageView.mas_bottom);
        make.right.equalTo(_headView.mas_right).mas_offset(-10);
        
    }];
    self.desLabel.font = [UIFont systemFontOfSize:12];
    self.total_threads.font = [UIFont systemFontOfSize:12];
    [self createCollectionView];
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    self.collectionView.layer.borderColor = COLORFORFREETRAVEL.CGColor;
    self.collectionView.layer.borderWidth = 1;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(80, 50);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.headView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[BBSCollectionViewCell class] forCellWithReuseIdentifier:@"BBSCollectionViewCell"];
}

- (void)handleData{
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_info?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&fid=%ld&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.5&v=1",self.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.forumModel = [ModelOfForum modelWithDic:result];
        
        [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=25&delcache=1&forum_id=%ld&forum_type=new&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.5&v=1 ",self.ID] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
            self.thread_listModel = [ModelOfForum_thread_list modelWithDic:result];
            
            [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.thread_listModel.data.photo]];
            self.desLabel.text = self.thread_listModel.data.Description;
            self.nameLabel.text = self.thread_listModel.data.name;
            self.total_threads.text = [NSString stringWithFormat:@"%ld个帖子",self.thread_listModel.data.total];
            [self.tableView reloadData];
            [self.collectionView reloadData];
        } andFailureBlock:^(NSError *error) {
            
        }];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.forumModel.data.types.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BBSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BBSCollectionViewCell" forIndexPath:indexPath];
    ModelOfForumTypes *model = self.forumModel.data.types[indexPath.row];
    cell.label.text = model.name;
    if (indexPath.row == self.oldIndexPath.row) {
        cell.label.backgroundColor = COLORFORFREETRAVEL;
        cell.label.textColor = [UIColor whiteColor];
    }else{
        cell.label.backgroundColor = [UIColor whiteColor];
        cell.label.textColor = [UIColor blackColor];
    }
//    NSLog(@"%ld",self.currentInteger);
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.thread_listModel.data.entry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfHotCommunity *cell = [tableView dequeueReusableCellWithIdentifier:@"BBSTableViewCell"];
    ModelOfForum_thread_listEntry *model = self.thread_listModel.data.entry[indexPath.row];
    [cell.imageViewOfAvatar sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    if (model.photoarr.count == 0) {
        [cell.imageViewOfFirstBigpic_arr sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    }else{
        if (model.photoarr.count >= 1) {
            [cell.imageViewOfFirstBigpic_arr sd_setImageWithURL:[NSURL URLWithString:model.photoarr[0]]];
            if (model.photoarr.count >= 2) {
                [cell.imageViewOfSecondBigpic_arr sd_setImageWithURL:[NSURL URLWithString:model.photoarr[1]]];
                if (model.photoarr.count == 3) {
                    [cell.imageViewOfThirdBigpic_arr sd_setImageWithURL:[NSURL URLWithString:model.photoarr[2]]];
                }
            }
        }
    }
    cell.labelOfReply_num.text = model.replys;
    cell.labelOfReply_time.text = [NSString stringWithFormat:@"%ld",model.publish_time];
    cell.labelOfAuthor.text = model.username;
    cell.labelOfTitle.text = model.title;
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BBSCollectionViewCell *cell = (BBSCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label.backgroundColor = COLORFORFREETRAVEL;
    cell.label.textColor = [UIColor whiteColor];
    BBSCollectionViewCell *oldCell = (BBSCollectionViewCell *)[collectionView cellForItemAtIndexPath:self.oldIndexPath];
    oldCell.label.backgroundColor = [UIColor whiteColor];
    oldCell.label.textColor  = [UIColor blackColor];
    self.oldIndexPath = indexPath;
  
    
    ModelOfForumTypes *model = self.forumModel.data.types[indexPath.item];
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=25&delcache=1&forum_id=%ld&forum_type=%@&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.5&v=1",self.ID,model.forum_type] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.thread_listModel = [ModelOfForum_thread_list modelWithDic:result];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WebViewOfCommendViewController *webVC = [[WebViewOfCommendViewController alloc]init];
    ModelOfForum_thread_listEntry *model = self.thread_listModel.data.entry[indexPath.row];
    webVC.url = model.view_url;
    [self.navigationController pushViewController:webVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
